force_key_down
This function causes a given key to assume the state of being held down.
bool force_key_down(int key)
Parameters:
key
One of the BGT key name constants, see appendix A for a full list.
Return value:
true on success, false on failure.
Remarks:
Forcing keys is useful for controlling certain aspects of games. For example, if you wanted a game to completely replay itself in the player's style, you might record the code and length of each key pressed in a previous game, and code the replay so that all the keys are forced up or down at the right moments, thereby completely simulating the player's actions.
The state of the key being forced is only recognised by the script calling the function - the key itself is not physically pressed down on the keyboard, nor is the key held down or indeed controlled in any way according to any other application.
To reset the state of the key so that it can again be controlled by the user, use either the reset_forced_key or reset_all_forced_keys function.
Example:
/*
This is a small example that plays the Windows ding every second while the space bar is pressed. If the f key is pressed, the script will force the space key down, thereby triggering the ding to play whether the space bar is physically pressed or not.
*/
void main()
{
sound ding;
timer time;
ding.load("c:/windows/media/ding.wav");
show_game_window("Ding Test");
while(true)
{
if((time.elapsed<1000)||(!key_down(KEY_SPACE)))
{
if((key_pressed(KEY_ESCAPE))||((key_down(KEY_LMENU))&&(key_pressed(KEY_F4))))
{
exit();
}
if(key_pressed(KEY_F))
{
force_key_down(KEY_SPACE);
}
wait(5);
continue;
}
time.restart();
ding.play();
}
}